package view;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
import java.util.*;
import java.io.*;

public class RabinUI extends javax.swing.JFrame {

// IMPORTANT: Source code between BEGIN/END comment pair will be regenerated
// every time the form is saved. All manual changes will be overwritten.
// BEGIN GENERATED CODE
	// member declarations
	javax.swing.JPanel RabinPanel = new javax.swing.JPanel();
	javax.swing.JLabel privateKey = new javax.swing.JLabel();
	javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
	javax.swing.JLabel qLabel = new javax.swing.JLabel();
	javax.swing.JTextArea pTextArea = new javax.swing.JTextArea();
	javax.swing.JTextArea qTextArea = new javax.swing.JTextArea();
	javax.swing.JLabel publicKey = new javax.swing.JLabel();
	javax.swing.JLabel nLabel = new javax.swing.JLabel();
	javax.swing.JTextArea nTextArea = new javax.swing.JTextArea();
	javax.swing.JButton keyButton = new javax.swing.JButton();
	javax.swing.JLabel plainText = new javax.swing.JLabel();
	javax.swing.JLabel cipherText = new javax.swing.JLabel();
	javax.swing.JButton plainTextClearButton = new javax.swing.JButton();
	javax.swing.JButton encryptButton = new javax.swing.JButton();
	javax.swing.JButton cipherTextClearButton = new javax.swing.JButton();
	javax.swing.JButton decryptButton = new javax.swing.JButton();
	javax.swing.JScrollPane pTextScrollPane = new javax.swing.JScrollPane();
	javax.swing.JTextArea plainTextArea = new javax.swing.JTextArea();
	javax.swing.JScrollPane cTextScrollPane = new javax.swing.JScrollPane();
	javax.swing.JTextArea cipherTextArea = new javax.swing.JTextArea();
// END GENERATED CODE
	KeySet keys = new KeySet(256);
	
	public RabinUI() {
	}

	public void initComponents() throws Exception {
// IMPORTANT: Source code between BEGIN/END comment pair will be regenerated
// every time the form is saved. All manual changes will be overwritten.
// BEGIN GENERATED CODE
		// the following code sets the frame's initial state

		RabinPanel.setSize(new java.awt.Dimension(1000, 800));
		RabinPanel.setLocation(new java.awt.Point(0, 0));
		RabinPanel.setVisible(true);
		RabinPanel.setLayout(null);
		RabinPanel.setForeground(java.awt.Color.blue);
		RabinPanel.setBackground(java.awt.Color.white);
		RabinPanel.add(privateKey);
		RabinPanel.add(jLabel1);
		RabinPanel.add(qLabel);
		RabinPanel.add(pTextArea);
		RabinPanel.add(qTextArea);
		RabinPanel.add(publicKey);
		RabinPanel.add(nLabel);
		RabinPanel.add(nTextArea);
		RabinPanel.add(keyButton);
		RabinPanel.add(plainText);
		RabinPanel.add(cipherText);
		RabinPanel.add(plainTextClearButton);
		RabinPanel.add(encryptButton);
		RabinPanel.add(cipherTextClearButton);
		RabinPanel.add(decryptButton);
		RabinPanel.add(pTextScrollPane);
		RabinPanel.add(cTextScrollPane);

		privateKey.setSize(new java.awt.Dimension(170, 20));
		privateKey.setLocation(new java.awt.Point(140, 30));
		privateKey.setVisible(true);
		privateKey.setText("Private Key");
		privateKey.setHorizontalAlignment(javax.swing.JLabel.CENTER);

		jLabel1.setSize(new java.awt.Dimension(20, 20));
		jLabel1.setLocation(new java.awt.Point(20, 90));
		jLabel1.setVisible(true);
		jLabel1.setText("p :");

		qLabel.setSize(new java.awt.Dimension(20, 20));
		qLabel.setLocation(new java.awt.Point(20, 180));
		qLabel.setVisible(true);
		qLabel.setText("q :");

		pTextArea.setSize(new java.awt.Dimension(320, 50));
		pTextArea.setColumns(75);
		pTextArea.setLocation(new java.awt.Point(70, 90));
		pTextArea.setVisible(true);
		pTextArea.setLineWrap(true);
		pTextArea.setToolTipText("Value of p");
		pTextArea.setEditable(false);
		pTextArea.setRows(4);

		qTextArea.setSize(new java.awt.Dimension(320, 50));
		qTextArea.setColumns(75);
		qTextArea.setLocation(new java.awt.Point(70, 180));
		qTextArea.setVisible(true);
		qTextArea.setLineWrap(true);
		qTextArea.setToolTipText("Value of q");
		qTextArea.setEditable(false);
		qTextArea.setRows(4);

		publicKey.setSize(new java.awt.Dimension(200, 20));
		publicKey.setLocation(new java.awt.Point(620, 30));
		publicKey.setVisible(true);
		publicKey.setText("Public Key");
		publicKey.setHorizontalAlignment(javax.swing.JLabel.CENTER);

		nLabel.setSize(new java.awt.Dimension(30, 20));
		nLabel.setLocation(new java.awt.Point(470, 100));
		nLabel.setVisible(true);
		nLabel.setText("n :");
		nLabel.setHorizontalAlignment(javax.swing.JLabel.CENTER);

		nTextArea.setSize(new java.awt.Dimension(440, 140));
		nTextArea.setColumns(75);
		nTextArea.setLocation(new java.awt.Point(530, 90));
		nTextArea.setVisible(true);
		nTextArea.setLineWrap(true);
		nTextArea.setToolTipText("Value of public key (n)");
		nTextArea.setEditable(true);
		nTextArea.setRows(8);

		keyButton.setSize(new java.awt.Dimension(200, 30));
		keyButton.setLocation(new java.awt.Point(340, 260));
		keyButton.setVisible(true);
		keyButton.setText("Generate Key");
		keyButton.setToolTipText("Press to generate new key");

		plainText.setSize(new java.awt.Dimension(170, 20));
		plainText.setLocation(new java.awt.Point(150, 320));
		plainText.setVisible(true);
		plainText.setText("Plain Text");
		plainText.setHorizontalAlignment(javax.swing.JLabel.CENTER);

		cipherText.setSize(new java.awt.Dimension(170, 20));
		cipherText.setLocation(new java.awt.Point(650, 320));
		cipherText.setVisible(true);
		cipherText.setText("Cipher Text");
		cipherText.setHorizontalAlignment(javax.swing.JLabel.CENTER);

		plainTextClearButton.setSize(new java.awt.Dimension(110, 30));
		plainTextClearButton.setLocation(new java.awt.Point(130, 720));
		plainTextClearButton.setVisible(true);
		plainTextClearButton.setText("Clear");
		plainTextClearButton.setToolTipText("Clear plain text box");

		encryptButton.setSize(new java.awt.Dimension(110, 30));
		encryptButton.setLocation(new java.awt.Point(280, 720));
		encryptButton.setVisible(true);
		encryptButton.setText("Encrypt   >>");

		cipherTextClearButton.setSize(new java.awt.Dimension(110, 30));
		cipherTextClearButton.setLocation(new java.awt.Point(630, 720));
		cipherTextClearButton.setVisible(true);
		cipherTextClearButton.setText("Clear");
		cipherTextClearButton.setToolTipText("Clear cipher text box");

		decryptButton.setSize(new java.awt.Dimension(110, 30));
		decryptButton.setLocation(new java.awt.Point(780, 720));
		decryptButton.setVisible(true);
		decryptButton.setText("<<  Decrypt");
		decryptButton.setToolTipText("Press to decrypt cipher text");

		pTextScrollPane.setSize(new java.awt.Dimension(440, 330));
		pTextScrollPane.setLocation(new java.awt.Point(50, 370));
		pTextScrollPane.setVisible(true);
		pTextScrollPane.getViewport().add(plainTextArea);

		plainTextArea.setColumns(35);
		plainTextArea.setVisible(true);
		plainTextArea.setToolTipText("Plain text to be encrypted");
		plainTextArea.setRows(2000);

		cTextScrollPane.setSize(new java.awt.Dimension(440, 330));
		cTextScrollPane.setLocation(new java.awt.Point(520, 370));
		cTextScrollPane.setVisible(true);
		cTextScrollPane.getViewport().add(cipherTextArea);

		cipherTextArea.setColumns(35);
		cipherTextArea.setVisible(true);
		cipherTextArea.setToolTipText("Cipher text to be decrypted");
		cipherTextArea.setRows(2000);

		setLocation(new java.awt.Point(0, 0));
		setResizable(false);
		setSize(new java.awt.Dimension(1000, 800));
		getContentPane().setLayout(null);
		setTitle("Rabin Cryptosystem");
		getContentPane().add(RabinPanel);


		keyButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				keyButtonMouseClicked(e);
			}
		});
		plainTextClearButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				plainTextClearButtonMouseClicked(e);
			}
		});
		encryptButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				encryptButtonMouseClicked(e);
			}
		});
		cipherTextClearButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				cipherTextClearButtonMouseClicked(e);
			}
		});
		decryptButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				decryptButtonMouseClicked(e);
			}
		});
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				thisWindowClosing(e);
			}
		});

// END GENERATED CODE
	}
  
  	private boolean mShown = false;
  	
	public void addNotify() {
		super.addNotify();
		
		if (mShown)
			return;
			
		// resize frame to account for menubar
		JMenuBar jMenuBar = getJMenuBar();
		if (jMenuBar != null) {
			int jMenuBarHeight = jMenuBar.getPreferredSize().height;
			Dimension dimension = getSize();
			dimension.height += jMenuBarHeight;
			setSize(dimension);
		}

		mShown = true;
	}

	// Close the window when the close box is clicked
	void thisWindowClosing(java.awt.event.WindowEvent e) {
		setVisible(false);
		dispose();
		System.exit(0);
	}
	
	public void keyButtonMouseClicked(java.awt.event.MouseEvent e) {
		keys = null;
		keys = new KeySet(256);
		pTextArea.setText(keys.getP().toString());
		qTextArea.setText(keys.getQ().toString());
		nTextArea.setText(keys.getN().toString());

	}
	
	public void plainTextClearButtonMouseClicked(java.awt.event.MouseEvent e) {
		plainTextArea.setText("");
	}
	
	public void encryptButtonMouseClicked(java.awt.event.MouseEvent e) {
	    //System.out.println("going to encrypt :" + nTextArea.getText());
		EncryptMessage enc = new EncryptMessage(new BigInteger(nTextArea.getText()), plainTextArea.getText());
		enc.encipher();
		//System.out.println("Encrypted " + enc.getCipherText());
		cipherTextArea.setText(enc.getCipherText());
	
	}
	
	public void cipherTextClearButtonMouseClicked(java.awt.event.MouseEvent e) {
		cipherTextArea.setText("");
	}
	
	public void decryptButtonMouseClicked(java.awt.event.MouseEvent e) {
	    //System.out.println("Decrypting message");
		StringBuffer temptext = new StringBuffer();	
		DecryptMessage dm;
		int count = 0;
		StringTokenizer c = new StringTokenizer(cipherTextArea.getText(),"\n");
		while (c.hasMoreTokens()){
			count ++;
			dm = null;
			dm = new DecryptMessage((new Roots(new BigInteger(pTextArea.getText()),new BigInteger(qTextArea.getText()), new BigInteger(c.nextToken()))).computeRoots());
			//System.out.println("adding "+dm.getPlainText());
			temptext.append(dm.getPlainText());
			temptext.append("\n");
			}
			
		plainTextArea.setText(temptext.toString());
		//System.out.println("count :" + count);
		
	}
	
	
	
	
	
	
}
